package com.kws.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kws.common.config.constant.MinioCommonBucket;
import com.kws.common.core.utils.StringUtils;
import com.kws.common.core.utils.bean.BeanUtils;
import com.kws.common.security.auth.AuthUtil;
import com.kws.common.security.utils.SecurityUtils;
import com.kws.merchant.domain.*;
import com.kws.merchant.domain.po.GoodsPo;
import com.kws.merchant.domain.vo.*;
import com.kws.merchant.mapper.GoodsMapper;
import com.kws.merchant.mapper.MerchantScoreMapper;
import com.kws.merchant.service.*;
import com.kws.merchant.util.FileUtil;
import com.kws.system.api.domain.SysDictData;
import com.kws.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : zhangbenting
 * @date : 2024/10/30 19:33
 * @Version: 1.0
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Value("${minio.ENDPOINT}")
    private String minioUrl;

    @Autowired
    private SpecificationService specificationService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private DeviceImgService deviceImgService;

    @Autowired
    private ShopCartService shopCartService;

    @Autowired
    private MerchantScoreMapper merchantScoreMapper;

    @Autowired
    private MerchantService merchantService;

    @Autowired
    private AttentionService attentionService;
    @Autowired
    private GoodsViewService goodsViewService;


    @Override
    public Page<GoodsVo> goodsList(GoodsPo goodsPo) {
        goodsPo.setPageSize((goodsPo.getPageSize() - 1) * goodsPo.getTotalPage());
        Long count = goodsMapper.goodsCount(goodsPo.getGoodsName(), goodsPo.getShopsName()
                , goodsPo.getType(), goodsPo.getShopsId(), goodsPo.getDeviceTag());
        Page<GoodsVo> page = new Page<>(goodsPo.getPageSize(), goodsPo.getTotalPage(), count);
        if (count > 0) {
            List<GoodsVo> goodsVoList = goodsMapper.goodsList(goodsPo.getGoodsName()
                    , goodsPo.getShopsName(), goodsPo.getType()
                    , goodsPo.getShopsId(), goodsPo.getDeviceTag(), goodsPo.getPageSize(), goodsPo.getTotalPage());
            for (GoodsVo goodsVo : goodsVoList) {
                if (StringUtils.isNotEmpty(goodsVo.getImg())) {
                    goodsVo.setImg(FileUtil.fixFileName() + goodsVo.getImg());
                }
            }
            page.setRecords(goodsVoList);
            return page;
        }
        return page;
    }

    @Override
    public GoodsInfoVo getGoodsInfo(GoodsPo goodsPo) {

        GoodsInfoVo goodsInfoVo = new GoodsInfoVo();

        //商品基本信息
        Device deviceById = deviceService.getById(goodsPo.getGoodsId());
        if (ObjectUtils.isNull(deviceById)) throw new RuntimeException("商品不存在");
        BeanUtils.copyProperties(deviceById, goodsInfoVo);

        //用户购物车商品数量
        long count = shopCartService.count(Wrappers.<ShopCartEntity>lambdaQuery()
                .eq(ShopCartEntity::getUserId, SecurityUtils.getUserId())
                .eq(ShopCartEntity::getType, 0));
        goodsInfoVo.setShoppingCartCount(count);

        //商品图片
        List<String> imgList = new ArrayList<>();
        List<DeviceImg> deviceImgList = deviceImgService.list(Wrappers.<DeviceImg>lambdaQuery()
                .eq(DeviceImg::getDeviceId, goodsPo.getGoodsId()));
        if (ObjectUtils.isNotNull(deviceImgList)) {
            for (DeviceImg deviceImg : deviceImgList) {
                if (StringUtils.isNotEmpty(deviceImg.getDeviceImg())) {
                    imgList.add(FileUtil.fixFileName() + deviceImg.getDeviceImg());
                }
            }
            goodsInfoVo.setImgArr(imgList);
        }

        //选择款式
        List<Specification> specificationList = specificationService.list(Wrappers.<Specification>lambdaQuery()
                .eq(Specification::getGoodsId, goodsPo.getGoodsId()).orderByDesc(Specification::getCreateTime));
        if (ObjectUtils.isNotNull(specificationList)) {
            goodsInfoVo.setSpecificationList(specificationList);
        }
        //商品浏览记录
        Long addGoodsViewId = goodsViewService.addGoodsView(deviceById.getMerchantId(), deviceById.getId(), 0);
        goodsInfoVo.setGoodsViewId(addGoodsViewId);
        return goodsInfoVo;
    }

    @Override
    public MerchantDetailVo getMerchantDetail(Long shopsId) {
        SysUser sysUser = AuthUtil.getLoginUser().getSysUser();
        Long userId = sysUser.getUserId();
        MerchantDetailVo merchantDetailVo = merchantScoreMapper.getMerchantDetail(shopsId);
        LambdaQueryWrapper<AttentionEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AttentionEntity::getAttentionTypeId, shopsId);
        wrapper.eq(AttentionEntity::getType, 2);
        long count = attentionService.count(wrapper);
        merchantDetailVo.setAttentionCount(count);
        wrapper.eq(AttentionEntity::getUserId, userId);
        merchantDetailVo.setIsCollect(ObjectUtils.isNotNull(attentionService.list(wrapper)));

        if (StringUtils.isNotEmpty(merchantDetailVo.getShopImg())) {
            merchantDetailVo.setShopImg(FileUtil.fixFileName() + merchantDetailVo.getShopImg());
        }
        if (StringUtils.isNotEmpty(merchantDetailVo.getBusinessLicense())) {
            List<String> strings = new ArrayList<>();
            strings.add(FileUtil.fixFileName() + merchantDetailVo.getBusinessLicense());
            merchantDetailVo.setMerchantLicense(strings);
        }
        return merchantDetailVo;
    }

    public Merchant getMerchantById(Long shopsId) {
        LambdaQueryWrapper<Merchant> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Merchant::getId, shopsId);
        wrapper.eq(Merchant::getEnable, "1");
        Merchant merchantById = merchantService.getOne(wrapper);
        return merchantById;
    }

    @Override
    public MerchantLicenseVo merchantLicense(Long shopsId) {
        Merchant merchantById = getMerchantById(shopsId);
        MerchantLicenseVo merchantLicenseVo = new MerchantLicenseVo();
        if (ObjectUtils.isNotNull(merchantById)) {
            if (StringUtils.isNotEmpty(merchantById.getBusinessLicenseUrl())) {
                merchantLicenseVo.setBusinessLicenseUrl(FileUtil.fixFileName() + merchantById.getBusinessLicenseUrl());
            }
            merchantLicenseVo.setBusinessScope(merchantById.getBusinessScope());
            merchantLicenseVo.setCompanyAddress(merchantById.getCompanyAddress());
            merchantLicenseVo.setCompanyName(merchantById.getCompanyName());
            merchantLicenseVo.setContactPerson(merchantById.getContactPerson());
        }
        return merchantLicenseVo;
    }

    @Override
    public List<SysDictData> getDictInfo(String dictType) {
        List<SysDictData> dictInfo = goodsMapper.getDictInfo(dictType);
        if (ObjectUtils.isNotNull(dictInfo)){
            return dictInfo;
        }
        return new ArrayList<SysDictData>();
    }

    @Override
    public List<MerchantDetailVo> getVideoNumberDevice() {
        List<MerchantDetailVo> videoNumber = goodsMapper.getVideoNumberDevice();
        videoNumber.forEach(item -> {
            if (StringUtils.isNotEmpty(item.getShopImg())) {
                item.setShopImg(FileUtil.fixFileName() + item.getShopImg());
            }
        });
        return videoNumber;
    }
}
